<template>
  <div class="library_box">
    <div class="tree_box">
      <el-tree
        :data="Treedata"
        accordion
        :props="defaultProps"
        highlight-current
        @node-click="handleNodeClick"
      >
      </el-tree>
    </div>
    <div class="app-container" style="width: 85%;">
      <el-form
        :model="queryParams"
        :rules="rules"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="关联课程" prop="classesId">
          <el-select
            v-model="queryParams.classesId"
            placeholder="请选择课程"
            filterable
            clearable
            size="small"
            style="width: 100%"
          >
            <el-option
              v-for="item in courseOptions"
              :key="item.index"
              :label="item.courseName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="卷库名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入卷库名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="卷库编号" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="请输入卷库编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="卷库状态" prop="state">
          <el-input
            v-model="queryParams.state"
            placeholder="请输入卷库状态"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置</el-button
          >
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['paper:library:add']"
            >新增</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['paper:library:edit']"
            >修改</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['paper:library:remove']"
            >删除</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            :loading="exportLoading"
            @click="handleExport"
            v-hasPermi="['paper:library:export']"
            >导出</el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table
        v-loading="loading"
        :data="libraryList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <!--      <el-table-column label="卷库状态" align="center" prop="id" />-->
        <!--      <el-table-column label="课程关联id" align="center" prop="classesId" />-->
        <el-table-column label="卷库名称" align="center" prop="name" />
        <el-table-column label="卷库编号" align="center" prop="code" />
        <el-table-column label="卷库描述" align="center" prop="other" />
        <el-table-column label="卷库状态" align="center" prop="state" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['paper:library:edit']"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['paper:library:remove']"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改卷库对话框 -->
      <el-dialog
        :title="title"
        :visible.sync="open"
        width="500px"
        append-to-body
      >
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="关联课程" prop="classesId">
            <el-select
              v-model="form.courseId"
              placeholder="请选择关联课程"
              clearable
              size="small"
              style="width: 100%"
            >
              <el-option
                v-for="item in courseOptions"
                :key="item.index"
                :label="item.courseName"
                :value="item.id"
              />
            </el-select>
            <!-- <el-input v-model="form.classesId" placeholder="请输入课程关联id" /> -->
          </el-form-item>
          <el-form-item label="卷库名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入卷库名称" />
          </el-form-item>
          <el-form-item label="卷库编号" prop="code">
            <el-input v-model="form.code" placeholder="请输入卷库编号" />
          </el-form-item>
          <el-form-item label="卷库描述" prop="other">
            <el-input
              v-model="form.other"
              type="textarea"
              placeholder="请输入内容"
            />
          </el-form-item>
          <el-form-item label="卷库状态" prop="state">
            <el-input v-model="form.state" placeholder="请输入卷库状态" />
          </el-form-item>
        </el-form>
        <div slot="footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  listLibrary,
  getLibrary,
  delLibrary,
  addLibrary,
  updateLibrary,
  exportLibrary,
  getQueryCourseTree,
} from "@/api/paper/library";
import { course } from "@/views/mixins/course/index";
import { validateCode } from "@/utils/formCheck";

export default {
  name: "Library",
  mixins: [course],
  data() {
    return {
      // 树数据
      Treedata: [],
      defaultProps: {
        children: "data",
        label: "courseName",
      },
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 卷库表格数据
      libraryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        classesId: null,
        courseId:null,
        name: null,
        code: null,
        other: null,
        state: null,
      },
      // 表单参数
      form: {
        courseId: null,
      },
      // 表单校验
      rules: {
        //校验卷库编号
        code: [{ validator: validateCode, trigger: "blur" }],
      },
      //课程信息
      courseOptions: [
        {
          id: "",
          courseName: "",
        },
      ],
    };
  },
  created() {
    this.getList();
    this.getQueryCourseTree();
  },
  methods: {
    resetCourseOptions() {
      this.courseOptions = [
        {
          id: "",
          name: "",
        },
      ];
    },
    /** 查询卷库列表 */
    getList() {
      this.loading = true;
      listLibrary(this.queryParams).then((response) => {
        this.libraryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getQueryCourseTree() {
      this.loading = true;
      getQueryCourseTree().then((res) => {
        this.Treedata = res;
        for (let i = 0; i < this.Treedata.length; i++) {
          if (this.Treedata[i].data != null) {
            for (let j = 0; j < this.Treedata[i].data.length; j++) {
              this.Treedata[i].data[j].data = [];
            }
          }
        }
        console.log(this.Treedata, "树");
        this.loading = false;
      });
    },
    // 树形目录点击后获取对应的学生信息
    handleNodeClick(data) {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        other: null,
        courseId:null
      };
      switch (data.flag) {
        case "startingUnit": // 一级
          break;
        case "course": // 二级
          this.queryParams.courseId = data.id;
          break;
        default:
          break;
      }
      /** 查询课程信息列表 */
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createBy: null,

        createTime: null,
        courseId: null,
        updateBy: null,
        updateTime: null,

        name: null,
        code: null,
        other: null,
        state: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.courseId = null
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加卷库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getLibrary(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改卷库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateLibrary(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLibrary(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除卷库编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delLibrary(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有卷库数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportLibrary(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss">
.library_box{
  display: flex;
  .tree_box{
    width: 13%;
    padding-top: 20px;
    max-height: 90vh;
    overflow-y: auto;
    &::-webkit-scrollbar {
      width: 5px;
    }
    &::-webkit-scrollbar-thumb {
      border-radius: 10px;
      -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
      box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
      background: rgba(0, 0, 0, 0.2);
    }
    &::-webkit-scrollbar-track {
      -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
      box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
      border-radius: 0;
      background: rgba(0, 0, 0, 0.1);
    }
  }
}
// .dialog-header {
//   //border-top: 1px solid #e8eaec;
//   margin-bottom: 20px;
//   padding: 8px 8px;
//   background-color: #336666;
//   color: #fffafa;
// }
// .dialog-footer {
//   border-top: 1px solid #336666;
//   padding-top: 5px;
//   text-align: center;
// }
// .sure {
//   background: #336666;
//   color: #fffafa;
// }
</style>
